package org.jpro.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.jpro.annotation.SystemControllerLog;
import org.jpro.annotation.SystemServiceLog;
import org.jpro.common.util.GenericsUtils;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

/**
 * <P>功能描述 : 实现Web层的日志切面 </P>
 * <P>File name : WebLogAspect </P>
 * <P>Author : 王泽浩 </P>
 * <P>E-Mail : 1028625100@qq.com </P>
 * <P>Date : 2017/2/25 上午11:36</P>
 */
@Slf4j
@Aspect
@Component
public class ServiceLogAspect {

    /**
     * <P>功能描述 : Controller层日志输出 </P>
     * <P>Method Name : beforeControllerLog </P>
     * <P>Author : 王泽浩 </P>
     * <P>E-Mail : 1028625100@qq.com </P>
     * <P>Date : 2017-03-01 15:09:53</P>
     * @param joinPoint 切入点
     * @throws ClassNotFoundException the class not found exception
     * @since 1.0
     */
    @Before("@annotation(org.jpro.annotation.SystemControllerLog)")
    public void beforeControllerLog(JoinPoint joinPoint) throws ClassNotFoundException {
        Object target = joinPoint.getTarget();
        Class<?> clazz = target.getClass();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        SystemControllerLog systemControllerLog = GenericsUtils.getAnnontationByJoinPoint(joinPoint, SystemControllerLog.class);
        if (systemControllerLog != null) {
            log.info("[{}] 请求参数:{}", systemControllerLog.value(), args);
        }
    }

    /**
     * <P>功能描述 : service层日志输出 </P>
     * <P>Method Name : beforeServiceLog </P>
     * <P>Author : 王泽浩 </P>
     * <P>E-Mail : 1028625100@qq.com </P>
     * <P>Date : 2017-03-01 15:09:53</P>
     * @param joinPoint 切入点
     * @throws ClassNotFoundException the class not found exception
     * @since 1.0
     */
    @Before("@annotation(org.jpro.annotation.SystemServiceLog)")
    public void beforeServiceLog(JoinPoint joinPoint) throws ClassNotFoundException {
        Object target = joinPoint.getTarget();
        Class<?> clazz = target.getClass();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        SystemServiceLog systemServiceLog = GenericsUtils.getAnnontationByJoinPoint(joinPoint, SystemServiceLog.class);
        if (systemServiceLog != null) {
            log.info("[{}] 请求参数:{}", systemServiceLog.value(), args);
        }
    }

}